package huawei;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author Jun jie Yi
 * 2022/4/7 22:33
 */

public class Test {
    public static void main(String[] args) {
        List<List<Integer>> wall = new ArrayList<>();
        wall.add(Arrays.asList(1, 2, 2, 1));
        wall.add(Arrays.asList(3, 1, 2));
        wall.add(Arrays.asList(1, 3, 2));
        wall.add(Arrays.asList(2, 4));
        wall.add(Arrays.asList(3, 1, 2));
        wall.add(Arrays.asList(1, 3, 1, 1));
        int min = leastBricks(wall);
        System.out.println(min);
    }

    public static int leastBricks(List<List<Integer>> wall) {
        //x轴的长度
        Integer x = wall.get(0).stream().parallel().reduce(0, (a, b) -> a + b, Integer::sum);
        //最少要穿过几个墙
        int min = wall.size();
        //标记
        int[] f = new int[wall.size()];
        //判断要穿过的墙的个数
        for (int i = 1; i < x; i++) {
            int num = 0;//记录穿墙的个数
            //对每面墙进行判断，记录需要穿墙的个数
            for (int k = 0; k < wall.size(); k++) {
                List<Integer> e = wall.get(k);
                if (e.get(f[k]) != i) {
                    num++;
                } else {
                    if (f[k] < e.size() - 1) {
                        f[k]++;//拿到下一个
                    }
                }
            }
            if (min > num) {
                min = num;
            }
        }
        return min;
    }
}
